using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace WX.WMS.Common
{
    /// <summary>
    /// GridView extension control
    /// @author:
    /// </summary>
    public class GridView : System.Web.UI.WebControls.GridView
    {
        /// <summary>
        /// if create a checkbox column
        /// </summary>
        private bool _showSelectBox = false;
        /// <summary>
        /// default checkbox ID
        /// </summary>
        private string _selectBoxKeyName = "CheckItem";
        /// <summary>
        /// default style of checkbox column
        /// </summary>
        private string _checkTemplateStyle = "CheckTemplateStyle";
        /// <summary>
        /// decide whether shows a radio box column in gridview
        /// </summary>
        private bool _showRadioBox = false;
        /// <summary>
        /// get selectedRadioId
        /// </summary>
        private string _selectedRadioId = null;
        /// <summary>
        /// the field name binding to radio column
        /// </summary>
        private string _radioBoxKeyName = "radioId";
        /// <summary>
        /// 
        /// </summary>
        private bool _enableEmptyContentRender = true;
        /// <summary>
        /// Will the head be showed when the data is null?
        /// </summary>
        public bool EnableEmptyContentRender
        {
            set { _enableEmptyContentRender = value; }
            get { return _enableEmptyContentRender; }
        }

        private string _EmptyDataCellCssClass;
        /// <summary>        
        /// the information cell style is displayed when the data is null
        /// </summary>
        public string EmptyDataCellCssClass
        {
            set { _EmptyDataCellCssClass = value; }
            get { return _EmptyDataCellCssClass; }
        }
        public bool ShowSelectBox
        {
            get
            {
                return _showSelectBox;
            }

            set
            {
                _showSelectBox = value;
            }
        }
        public string SelectBoxKeyName
        {
            get
            {
                return _selectBoxKeyName;
            }
            set
            {
                _selectBoxKeyName = value;
            }
        }

        public string CheckTemplateStyle
        {
            get
            {
                return _checkTemplateStyle;
            }
            set
            {
                _checkTemplateStyle = value;
            }
        }

        public bool ShowRadioBox
        {
            set
            {
                _showRadioBox = value;
            }
            get
            {
                return _showRadioBox;
            }
        }

        public string SelectedRadioId
        {
            set
            {
                _selectedRadioId = value;
            }
            get
            {
                return _selectedRadioId;
            }
        }

        public string RadioBoxKeyName
        {
            set { _radioBoxKeyName = value; }
            get { return _radioBoxKeyName; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            if (this.ShowSelectBox)
            {
                TemplateField dcf = new TemplateField();

                dcf.HeaderStyle.CssClass = CheckTemplateStyle;
                dcf.HeaderTemplate = new CheckHeaderTemplate("checkall");
                CheckTemplate checkTemplate = new CheckTemplate(this.SelectBoxKeyName);
                dcf.ItemTemplate = checkTemplate;
                dcf.ItemStyle.CssClass = CheckTemplateStyle;
                this.Columns.Insert(0, dcf);
            }

            if (this.ShowRadioBox)
            {
                TemplateField template = new TemplateField();

                RadioTemplate radioTemplate = new RadioTemplate(this.RadioBoxKeyName, this);
                template.ItemTemplate = radioTemplate;
                //template.ItemStyle.CssClass = CheckTemplateStyle;
                this.Columns.Insert(0, template);
            }
            base.OnInit(e);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns>null or string</returns>
        public string GetKeyValues
        {
            get
            {
                if (this.DataKeyNames.Length < 1)
                {
                    return "";
                }
                StringBuilder tmp = new StringBuilder();
                foreach (GridViewRow vr in this.Rows)
                {
                    if (vr.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox cb = (CheckBox)vr.FindControl(this.SelectBoxKeyName);
                        if (cb.Checked)
                        {
                            tmp.Append(this.DataKeys[vr.RowIndex].Value.ToString());
                            tmp.Append(",");
                        }
                    }
                }
                if (tmp.Length > 0)
                    tmp.Remove(tmp.Length - 1, 1);
                return tmp.ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ArrayList GetCheckedList()
        {
            ArrayList list = new ArrayList();
            if (this.DataKeyNames.Length < 1) return null;
            foreach (GridViewRow vr in this.Rows)
            {
                if (vr.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)vr.FindControl(this.SelectBoxKeyName);
                    if (cb.Checked)
                    {
                        list.Add(this.DataKeys[vr.RowIndex].Value.ToString());
                    }
                }
            }
            if (list.Count > 0) return list;
            else return null;
        }


        /// <summary>
        /// output information when the data is null
        /// </summary>
        /// <param name="writer"></param>
        protected virtual void RenderEmptyContent(HtmlTextWriter writer)
        {
            Table t = new Table(); //create a table
            if (this.Caption != null)
                t.Caption = this.Caption;
            t.CssClass = this.CssClass; //copy all property
            t.GridLines = this.GridLines;
            t.BorderStyle = this.BorderStyle;
            t.BorderWidth = this.BorderWidth;
            t.CellPadding = this.CellPadding;
            t.CellSpacing = this.CellSpacing;

            t.HorizontalAlign = this.HorizontalAlign;

            t.Width = this.Width;

            t.CopyBaseAttributes(this);


            TableHeaderRow row = new TableHeaderRow();
            row.CssClass = this.HeaderStyle.CssClass;
            t.Rows.Add(row);

            foreach (DataControlField f in this.Columns) //generate table header
            {
                if (f.Visible == true)
                {

                    TableHeaderCell cell = new TableHeaderCell();
                    cell.Text = f.HeaderText;
                    cell.Width = f.HeaderStyle.Width;
                    cell.Height = f.HeaderStyle.Height;
                    cell.CssClass = f.HeaderStyle.CssClass;
                    row.Cells.Add(cell);
                }
            }

            TableRow row2 = new TableRow();
            t.Rows.Add(row2);

            TableCell msgCell = new TableCell();
            msgCell.CssClass = this._EmptyDataCellCssClass;

            if (this.EmptyDataTemplate != null) //the second row, use the template
            {
                this.EmptyDataTemplate.InstantiateIn(msgCell);
            }
            else //the second row, use the EmptyDataText
            {
                msgCell.Text = this.EmptyDataText;
            }

            msgCell.HorizontalAlign = HorizontalAlign.Center;
            msgCell.ColumnSpan = this.Columns.Count;

            row2.Cells.Add(msgCell);

            t.RenderControl(writer);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (_enableEmptyContentRender && (this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow))
            {
                RenderEmptyContent(writer);
            }
            else
            {
                base.Render(writer);
            }
        }

    }

    public class CheckTemplate : ITemplate
    {
        private string colname;
        public CheckTemplate(string colname)
        {
            this.colname = colname;
        }
        public void InstantiateIn(Control container)
        {
            CheckBox cb = new CheckBox();
            cb.ID = this.colname;
            container.Controls.Add(cb);
        }

    }
    public class CheckHeaderTemplate : ITemplate
    {
        private string colname = "checkall";
        public CheckHeaderTemplate(string colname)
        {
            this.colname = colname;
        }
        public void InstantiateIn(Control container)
        {
            CheckBox cb = new CheckBox();
            cb.ID = this.colname;
            cb.Attributes.Add("onclick", "CheckAll(this)");
            container.Controls.Add(cb);
        }

    }

    public class RadioTemplate : ITemplate
    {
        private string _colName;
        private GridView _gridview;

        public RadioTemplate(string colName, GridView view)
        {
            this._colName = colName;
            this._gridview = view;
        }

        #region ITemplate Members

        public void InstantiateIn(Control container)
        {
            RadioButton rad = new RadioButton();
            rad.ID = this._colName;
            rad.CheckedChanged += new EventHandler(rad_CheckedChanged);
            container.Controls.Add(rad);
        }

        void rad_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton selectedItem = (RadioButton)sender;

            foreach (GridViewRow row in _gridview.Rows)
            {
                RadioButton radio = (RadioButton)row.FindControl(this._colName);
                if (radio != selectedItem)
                {
                    radio.Checked = false;
                }
                else
                {
                    _gridview.SelectedRadioId = _gridview.DataKeys[row.RowIndex].Value.ToString();
                }
            }
        }

        #endregion
    }
}
